﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongCat;
using QuanLyKho.PhongGiaCong;


namespace QuanLyKho.PhongMayKhoan
{
    public partial class SanPhamMayKhoan : UserControl
    {
        public SanPhamMayKhoan()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
            SetPropertiesColumns();
        }

        public void LoadData()
        {
            DataTable dtMau = DataAll.SelectAll("Cat inner join PhongMau_SanPham on Cat.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],Cat.[ID],Cat.[IdSanPham],Cat.[NgayLapLenh],Cat.[NguoiCapNhatLenh],Cat.[SoLuong],Cat.[NgayGuiLenh],Cat.[NgaySuaLenh],Cat.[TrangThai],Cat.[NgayChungTu],Cat.[NgayHoachToan],Cat.[SoChungTu],Cat.[GhiChu],Cat.[KhoDen],Cat.[KhoDi],Cat.[SXTuNgay],Cat.[SXDenNgay]",
                    "WHERE Cat.TrangThai = " + Convert.ToInt32(TrangThai.Cat_GuiMayKhoan));
            gridCat.DataSource = dtMau;
            DataTable data = DataAll.SelectAll("GiaCong inner join PhongMau_SanPham on GiaCong.IdSanPham = PhongMau_SanPham.IdSanPham",
                "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],GiaCong.[ID],GiaCong.[IdSanPham],GiaCong.[NgayLapLenh],GiaCong.[NguoiCapNhatLenh],GiaCong.[SoLuong],GiaCong.[NgayGuiLenh],GiaCong.[NgaySuaLenh],GiaCong.[TrangThai],GiaCong.[NgayChungTu],GiaCong.[NgayHoachToan],GiaCong.[SoChungTu],GiaCong.[GhiChu],GiaCong.[KhoDen],GiaCong.[KhoDi],GiaCong.[SXTuNgay],GiaCong.[SXDenNgay]",
                "WHERE GiaCong.TrangThai = " + Convert.ToInt32(TrangThai.MayKhoan_TaoMoi)
                + " OR GiaCong.TrangThai = " + Convert.ToInt32(TrangThai.MayKhoan_KCSDaDuyet)
                + " OR GiaCong.TrangThai = " + Convert.ToInt32(TrangThai.MayKhoan_KCSKhongDuyet)
                + " OR GiaCong.TrangThai = " + Convert.ToInt32(TrangThai.MayKhoan_GuiDi));
            gridMayKhoan.DataSource = data;
        }

        public void SetPropertiesGrid()
        {
            gvMayKhoan.BestFitColumns();
            gvMayKhoan.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvMayKhoan.Appearance.SelectedRow.BackColor = Color.Empty;
            gvMayKhoan.Appearance.SelectedRow.Options.UseBackColor = true;
            gvMayKhoan.OptionsView.ShowGroupPanel = false;
            gvMayKhoan.OptionsView.ShowFooter = true;
            gvMayKhoan.OptionsFind.AlwaysVisible = true;
            gvMayKhoan.Columns["ID"].Visible = false;

            gvCat.BestFitColumns();
            gvCat.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvCat.OptionsBehavior.Editable = false;
            gvCat.OptionsSelection.MultiSelect = true;
            gvCat.OptionsView.ShowGroupPanel = false;
            gvCat.OptionsView.ShowFooter = true;
            gvCat.OptionsFind.AlwaysVisible = true;
            gvCat.Columns["ID"].Visible = false;

        }

        public void SetPropertiesColumns()
        {
            gcGuiKCS.Width = 40;
            gcGuiKCS.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaSanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            //gcNguoiLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgaySuaLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            //gcS.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcM.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcXL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcTongCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcDMTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcSLCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
        }

        private void gvCat_DoubleClick(object sender, EventArgs e)
        {
            if (gvCat.FocusedRowHandle >= 0)
            {
                var dataview = (DataView)gvCat.DataSource;
                var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvCat.FocusedRowHandle]["SoChungTu"]
                    + "' AND TrangThai=" + Convert.ToInt32(TrangThai.Cat_MayKhoanDaNhan));
                if (data.Rows.Count <= 0)
                {
                    DataAll.InsertAll("HanhTrinh", dataview[gvCat.FocusedRowHandle]["MaSanPham"]
                                + "," + Convert.ToInt32(PhongBan.PhongMayKhoan)
                                + "," + dataview[gvCat.FocusedRowHandle]["SoChungTu"]
                                + "," + Convert.ToInt32(TrangThai.Cat_MayKhoanDaNhan)
                                + "," + Main.UserID
                                + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                                + "," + dataview[gvCat.FocusedRowHandle]["SoLuong"]
                                + "," + Convert.ToDateTime(dataview[gvCat.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                                + "," + Convert.ToDateTime(dataview[gvCat.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                }
                CapNhatMayKhoan_Popup.IdCat = gvCat.Columns["ID"].View.GetRowCellDisplayText(gvCat.FocusedRowHandle, "ID");
                var lss = new CapNhatMayKhoan_Popup();
                lss.ShowDialog();
                LoadData();
                CapNhatMayKhoan_Popup.IdCat = "0";
            }
        }

        private void gvCat_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvMayKhoan_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvMayKhoan_DoubleClick(object sender, EventArgs e)
        {
            if (gvMayKhoan.FocusedRowHandle >= 0)
            {
                string a = gvMayKhoan.GetRowCellValue(gvMayKhoan.FocusedRowHandle, "TrangThai").ToString();
                string b = Convert.ToInt32(TrangThai.MayKhoan_TaoMoi).ToString();
                if (a == b)
                {
                    if (gvMayKhoan.FocusedRowHandle >= 0)
                    {
                        CapNhatMayKhoan_Popup.MaID =
                            gvMayKhoan.Columns["ID"].View.GetRowCellDisplayText(gvMayKhoan.FocusedRowHandle, "ID");
                        var lss = new CapNhatMayKhoan_Popup();
                        lss.ShowDialog();
                        LoadData();
                        CapNhatMayKhoan_Popup.MaID = "0";
                    }
                }
            }
        }

        private void btGuiLenh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvMayKhoan.DataSource;
            string a = gvMayKhoan.GetRowCellValue(gvMayKhoan.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.MayKhoan_TaoMoi).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("GiaCong", "TrangThai =" + Convert.ToInt32(TrangThai.MayKhoan_GuiDi) + ",NgayGuiLenh ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE ID =" + dataview[gvMayKhoan.FocusedRowHandle]["ID"]);

                    MessageBox.Show(message.Equals("success") ? "Gửi KCS thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    DataAll.InsertAll("HanhTrinh", dataview[gvMayKhoan.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongMayKhoan)
                            + "," + dataview[gvMayKhoan.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.MayKhoan_GuiDi)
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvMayKhoan.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvMayKhoan.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvMayKhoan.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                    LoadData();
                }
            }
        }

        private void gvMayKhoan_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                GridView View = sender as GridView;
                string trangthai = View.GetRowCellDisplayText(e.RowHandle, View.Columns["TrangThai"]);
                if (trangthai == "Phòng may khoán tạo mới")
                {
                    e.Appearance.BackColor = Color.Empty;
                }
                else if (trangthai == "Đã gửi KCS")
                {
                    e.Appearance.BackColor = Color.SandyBrown;
                }
                else if (trangthai == "Phòng KCS không duyệt")
                {
                    e.Appearance.BackColor = Color.Salmon;
                }
                else
                {
                    e.Appearance.BackColor = Color.DarkSeaGreen;
                }
            }
        }

        private void gvCat_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

        private void btnHuy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvCat.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi trả ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("LenhSanXuat", "TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho) + " WHERE SoChungTu ='" + dataview[gvCat.FocusedRowHandle]["SoChungTu"] + "'");
                if (message.Equals("success"))
                {
                    DataAll.Delete("Cat", "ID =" + dataview[gvCat.FocusedRowHandle]["ID"]);
                }
                MessageBox.Show(message.Equals("success") ? "Hủy thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            LoadData();
        }

        private void btnHuyMayKhoan_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvMayKhoan.DataSource;
            string a = gvMayKhoan.GetRowCellValue(gvMayKhoan.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.MayKhoan_TaoMoi).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn hủy ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("Cat", "TrangThai = " + Convert.ToInt32(TrangThai.Cat_GuiMayKhoan) + " WHERE SoChungTu ='" + dataview[gvMayKhoan.FocusedRowHandle]["SoChungTu"] + "'");
                    if (message.Equals("success"))
                    {
                        DataAll.Delete("GiaCong", "ID =" + dataview[gvMayKhoan.FocusedRowHandle]["ID"]);
                    }
                    MessageBox.Show(message.Equals("success") ? "Hủy thành công" : message, "Message",
                                       MessageBoxButtons.OK,
                                       message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                }
            }
            LoadData();
        }
    }
}
